package com.lagou.dao;

import com.lagou.dao.UserDao;
import com.lagou.model.Student;
import com.lagou.model.User;
import com.lagou.utils.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserDaoImpl implements UserDao {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    @Override
    public User userLogin(User user) {

        try {
            // 1.获取数据库连接
            con = DbUtil.getConnection();

            // 2.执行sql语句后获取结果并返回
            ps = con.prepareStatement("select * from t_user where userName= ? and password = ?");
            ps.setString(1,user.getUserName());
            ps.setString(2,user.getPassWord());
            // 3.获得结果集
            rs = ps.executeQuery();
            if (rs.next()){
                User userInfo = new User(rs.getInt("id"),rs.getString("userName"),rs.getString("password"));
                return userInfo;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4.释放相关资源
            DbUtil.closeResouce(con,ps,rs);
        }
        return null;
    }

    @Override
    public List<Student> showAllUser() {
        List<Student> list = new ArrayList<Student>();
        try {
            // 1.获取数据库连接
            con = DbUtil.getConnection();
            // 2.执行sql语句后获取结果并返回
            ps = con.prepareStatement("select * from t_student ");
            // 3.获得结果集
            rs = ps.executeQuery();
            while (rs.next()){
                Student student = new Student(rs.getInt("id"),rs.getString("name"),rs.getString("sex"),rs.getDate("birthday"),rs.getString("mail"),rs.getString("remark"));
                //User userInfo = new User(rs.getInt("id"),rs.getString("userName"),rs.getString("password"));
                list.add(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4.释放相关资源
            DbUtil.closeResouce(con,ps,rs);
        }
        return list;
    }

    @Override
    public boolean addStudent(Student student) {
        try {
            con = DbUtil.getConnection();
            int id = student.getId().intValue();
            String name = student.getName();
            String sex = student.getSex();
            Date birthday = student.getBirthday();
            String mail = student.getMail();
            String remark = student.getRemark();
            ps = con.prepareStatement("insert into t_student values (?,?,?,?,?,?)");
            ps.setInt(1,id);
            ps.setString(2,name);
            ps.setString(3,sex);
            ps.setDate(4, new java.sql.Date(birthday.getTime()));
            ps.setString(5,mail);
            ps.setString(6,remark);
            int rs = ps.executeUpdate();
            if(rs>0){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {

            DbUtil.closeResouce(con,ps);
        }
        return false;
    }

    @Override
    public boolean deleteStudent(Integer id) {
        try {
            con = DbUtil.getConnection();
            ps = con.prepareStatement("delete from t_student where id = ?");
            ps.setInt(1,id);
            int rs = ps.executeUpdate();
            if(rs>0){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtil.closeResouce(con,ps);
        }
        return false;
    }

    @Override
    public boolean updateStudent(Student student) {
        try {
            con = DbUtil.getConnection();
            int id = student.getId().intValue();
            String name = student.getName();
            String sex = student.getSex();
            Date birthday = student.getBirthday();
            String mail = student.getMail();
            String remark = student.getRemark();
            ps = con.prepareStatement("update t_student set name=?,sex=?,birthday=?,mail=?,remark=? where id = ?");
            ps.setString(1,name);
            ps.setString(2,sex);
            ps.setDate(3, new java.sql.Date(birthday.getTime()));
            ps.setString(4,mail);
            ps.setString(5,remark);
            ps.setInt(6,id);
            int rs = ps.executeUpdate();
            if(rs>0){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {

            DbUtil.closeResouce(con,ps);
        }
        return false;
    }

    @Override
    public Student findStudentById(Integer id) {
        try {
            // 1.获取数据库连接
            con = DbUtil.getConnection();
            // 2.执行sql语句后获取结果并返回
            ps = con.prepareStatement("select * from t_student where id = ? ");
            ps.setInt(1,id);
            // 3.获得结果集
            rs = ps.executeQuery();
            while (rs.next()){
               return new Student(rs.getInt("id"),rs.getString("name"),rs.getString("sex"),rs.getDate("birthday"),rs.getString("mail"),rs.getString("remark"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4.释放相关资源
            DbUtil.closeResouce(con,ps,rs);
        }
        return null;
    }
}
